package com.dews.web.controller.train;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.dews.common.core.controller.BaseController;
import com.dews.common.core.domain.BaseResponse;
import com.dews.common.core.domain.model.LoginUser;
import com.dews.common.utils.SecurityUtils;
import com.dews.information.domain.entity.StudentInfo;
import com.dews.information.service.IClazzInfoService;
import com.dews.information.service.IStudentInfoService;
import com.dews.train.domain.entity.TrainingProgram;
import com.dews.train.domain.vo.ProgramTeachPlanVo;
import com.dews.train.domain.vo.TalentCultivationPlanVo;
import com.dews.train.service.IProgramTeachPlanService;
import com.dews.train.service.ITrainingProgramService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 人才培养方案
 * @version 1.0.0
 * @date 2025/1/18
 */
@Api(value = "人才培养方案", tags = {"人才培养方案" })
@RestController
@RequestMapping("/talent/cultivationPlan")
public class TalentCultivationPlanController  extends BaseController {

    private ITrainingProgramService trainingProgramService;
    private IProgramTeachPlanService programTeachPlanService;
    private IClazzInfoService clazzInfoService;
    private IStudentInfoService studentInfoService;

    public TalentCultivationPlanController(ITrainingProgramService trainingProgramService,
                                           IProgramTeachPlanService programTeachPlanService,
                                           IClazzInfoService clazzInfoService,
                                           IStudentInfoService studentInfoService) {
        this.trainingProgramService = trainingProgramService;
        this.programTeachPlanService = programTeachPlanService;
        this.clazzInfoService = clazzInfoService;
        this.studentInfoService = studentInfoService;
    }
    
    @ApiOperation(value = "根据用户学院，专业年级查询人才培养方案信息")
    @PreAuthorize("@ss.hasPermi('train:cultivationPlan:info')")
    @GetMapping("/info")
    public BaseResponse getCultivationPlanByUserInfo() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (ObjectUtil.isEmpty(loginUser)) {
            return error("当前用户不存在，支持查询人才培养方案信息！");
        }
        StudentInfo studentInfo = studentInfoService.getOne(
                Wrappers.lambdaQuery(StudentInfo.class)
                        .eq(StudentInfo::getUserId, loginUser.getUserId())
        );
        if (ObjectUtil.isEmpty(studentInfo)) {
            return error("当前用户不存在，不支持查询人才培养方案信息！");
        }
        TrainingProgram trainingProgram = trainingProgramService.getOne(
                Wrappers.lambdaQuery(TrainingProgram.class)
                        .eq(TrainingProgram::getDepId, loginUser.getDeptId())
                        .eq(TrainingProgram::getSubjectId, studentInfo.getSubjectId())
                        .eq(TrainingProgram::getGrade, studentInfo.getGrade())
                        .eq(TrainingProgram::getIsEnable, 1)
        );
        // 查询教学计划
        List<ProgramTeachPlanVo> programTeachPlanVoList = programTeachPlanService.selectProgramTeachPlanList(trainingProgram.getId());
        return success(
                TalentCultivationPlanVo.builder()
                        .trainingProgram(trainingProgram)
                        .programTeachPlanVoList(programTeachPlanVoList)
                        .build()
        );
    }
}
